'''------------------------------------------------------------------------------
# Project:     World Exposition game
# Name:        enemy.py
# Purpose:     Enemy for game World Exposition
# 
# Authors:     Sam Milender, Calvin Berger
# Main Author: Sam Milender
#
# Created:     Some time in late April, 2013
# Copyright:   (c) Sam Milender 2012
# Licence:     New BSD
#------------------------------------------------------------------------------
'''
import pygame, math, sys, random

class Enemy(pygame.sprite.Sprite):
    def __init__(self, speed, position, screenSize, images, health):
        pygame.sprite.Sprite.__init__(self, self.containers)
        self.images = []
        self.images = [pygame.image.load(images)]
        self.frame = 0
        self.maxFrame = len(self.images)-1
        self.image = self.images[self.frame]
        self.rect = self.image.get_rect()
        self.speed = speed
        self.realx = position[0]
        self.realy = position[1]
        self.xOffset = 0
        self.yOffset = 0
        self.place(position)
        self.living = True
        self.didHit = False
        self.screenSize = self.screenWidth, self.screenHeight = screenSize
        self.health = health
        
        
    def  __str__(self):
#        return "I'm a ball " + str(self.rect.center) + str(self.speed) + str(self.living)
        pass
     
    def place(self, position):
        self.rect.center = position
        #print "I've moved to", position
        
    def move(self):
        self.realx += self.speed[0]
        self.realy += self.speed[1]
        self.place([self.realx-self.xOffset, self.realy-self.yOffset])
        #print "I've moved", self.speed
        
    def distToPoint(self, pt):
        x1 = self.rect.center[0]
        x2 = pt[0]
        y1 = self.rect.center[1]
        y2 = pt[1]
        return math.sqrt(((x2-x1)**2)+((y2-y1)**2))
    
    def wallCollide(self):
        if (self.rect.left < 0 
            or self.rect.right > self.screenWidth):
            self.speed[0] = self.speed[0]*-1
        if (self.rect.top < 0 
            or self.rect.bottom >self.screenHeight):
            self.speed[1] = self.speed[1]*-1
            
    def fenceCollide(self, fence):
        print "Fence detected me!! Hee Hee"
        if (self.rect.right > fence.rect.left 
        and self.rect.left < fence.rect.right):
            if (self.rect.bottom > fence.rect.top and 
                self.rect.top < fence.rect.bottom):
                    self.speed[0] = self.speed[0] * -1
                    self.speed[1] = self.speed[1] * -1
   
    def playerCollide(self, player):
        if (self != player 
            and not self.didHit 
            and not player.didHit):
                self.didHit = True
                player.didHit = True
                self.speed[0] = self.speed[0] * -1
                self.speed[1] = self.speed[1] * -1
                player.speed[0] = player.speed[0] * -1
                player.speed[1] = player.speed[1] * -1
                
    def bulletCollide(self, bullet):
        if (self != bullet 
            and not self.didHit 
            and not bullet.didHit):
                self.didHit = True
                bullet.didHit = True
                self.health = self.health - 1
                if self.health == 0:
                    self.living = False

    def update(*args):
        self = args[0]
        self.xOffset = args[1]
        self.yOffset = args[1]
        self.didHit = False
        if not self.living:
            self.kill()
        self.move()
        self.wallCollide()